package com.android.equipment.dao.operate;

import com.android.equipment.ObjectBox;
import com.android.equipment.dao.HistoryAlam;
import com.android.equipment.dao.HistoryAlam_;
import com.blankj.utilcode.util.ToastUtils;

import java.util.List;

import io.objectbox.Box;
import io.objectbox.query.Query;

public class DaoHistoryAlam {
    static Box<HistoryAlam> historyAlamBox;
    private static DaoHistoryAlam instance;

    private DaoHistoryAlam() {
    }

    public static DaoHistoryAlam getInstance() {
        if (instance == null) {
            instance = new DaoHistoryAlam();
            historyAlamBox = ObjectBox.get().boxFor(HistoryAlam.class);
        }
        return instance;
    }

    public void addHistoryAlam(HistoryAlam historyAlam) {
        historyAlamBox.put(historyAlam);
    }

    public void deleteHistoryAlam(HistoryAlam historyAlam) {
        historyAlamBox.remove(historyAlam);
    }

    public void updateHistoryAlam(HistoryAlam historyAlam) {
        historyAlamBox.put(historyAlam);
    }

    public HistoryAlam getHistoryAlam(long id) {
        return historyAlamBox.get(id);
    }

    public List<HistoryAlam> getAllHistoryAlam() {
        return historyAlamBox.getAll();
    }

    public void updateHistoryAlamByRealTimeId(long workId, long realTimeAlamId, String recover_time) {
        Query<HistoryAlam> query = historyAlamBox.query()
                .equal(HistoryAlam_.work_id, workId)
                .equal(HistoryAlam_.real_time_alam_id, realTimeAlamId)
                .build();
        List<HistoryAlam> historyAlamsList = query.find();
        if (historyAlamsList.size() != 1){
            ToastUtils.showShort("数据库中存在多个相同id的数据，请检查");
        }else {
            HistoryAlam historyAlam = historyAlamsList.get(0);
            historyAlam.setRecover_time(recover_time);
            historyAlamBox.put(historyAlam);
        }
        query.close();
    }

    public void deleteAllHistory(){
        historyAlamBox.removeAll();
    }


    public List<HistoryAlam> getHistoryAlamByWorkId(long workId){
    	Query<HistoryAlam> query = historyAlamBox.query()
    			.equal(HistoryAlam_.work_id, workId)
    			.build();
    	List<HistoryAlam> historyAlamList = query.find();
    	query.close();
        return historyAlamList;
    }
}
